const path = require('path');

const { CleanWebpackPlugin } = require('clean-webpack-plugin');

const TerserPlugin = require('terser-webpack-plugin');

const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const webpack = require('webpack');


const getHtmlConfig = function (name) {
    return {
        template: './src/view/' + name + '.html',
        filename: 'view/' + name + '.html',
        inject: true,
        hash: true,
        chunks: ['common', name],
        minify: {
            collapseWhitespace: false
        }
    }
}

var config = {
    //定义入口
    entry: {
        'common': ['./src/page/common/index.js'],
        'index': ['./src/page/index/index.js'],
        'home': ['./src/page/home/index.js'],
        'coupons_manage': ['./src/page/coupons_manage/index.js'],
        'new_coupon': ['./src/page/new_coupon/index.js'],
        'user_manage': ['./src/page/user_manage/index.js'],
        'sellers_manage': ['./src/page/sellers_manage/index.js'],
        'shops_manage': ['./src/page/shops_manage/index.js']
    },
    output: {
        filename: 'js/[name].js',
        path: path.resolve(__dirname, 'dist'),
        globalObject: 'this'
    },
    devServer: {
        port: 8999,
        static: path.resolve(__dirname, 'dist'),
          
    },
    stats: {
        warnings: false
    },
    performance: {
        hints: false
    },
      
    module: {
        rules: [
            {
                test: /\.css$/, 
                use: [

                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.less$/,
                use: [

                    'style-loader',
                    'css-loader',
                    'less-loader'
                ]
            },
            {
                test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
                use: {
                    loader: "file-loader"
                }
            },
            {
                test: /\.(woff|woff2)$/,
                use: {
                    loader: "url-loader",
                    options: {
                        prefix: "font",
                        limit: 5000,
                        outputPath: "fonts",
                        esModule: false
                    }
                }
            },
            {
                test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                use: {
                    loader: "url-loader",
                    options: {
                        limit: 10000,
                        mimetype: "application/octet-stream",
                        outputPath: 'fonts',
                        esModule: false
                    }
                }
            },
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 10000,
                            mimetype: "image/svg+xml",
                            outputPath: "svg",
                            esModule: false
                        }
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'image',
                            limit: 10,
                            esModule: false
                        }
                    }
                ],
                type: 'javascript/auto'
            },

            {
                test: /\.html$/,
                use: {
                    loader: 'html-withimg-loader',

                }
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),   
        new MiniCssExtractPlugin({
            filename: 'css/[name].css'
        }),
        new HtmlWebpackPlugin(getHtmlConfig('index')),
        new HtmlWebpackPlugin(getHtmlConfig('home')),
        new HtmlWebpackPlugin(getHtmlConfig('coupons_manage')),
        new HtmlWebpackPlugin(getHtmlConfig('items_manage')),
        new HtmlWebpackPlugin(getHtmlConfig('new_items')),
        new HtmlWebpackPlugin(getHtmlConfig('new_coupon')),
        new HtmlWebpackPlugin(getHtmlConfig('new_productCate')),
        new HtmlWebpackPlugin(getHtmlConfig('new_swiper')),
        new HtmlWebpackPlugin(getHtmlConfig('orders_manage')),
        new HtmlWebpackPlugin(getHtmlConfig('orders_moreinfo')),
        new HtmlWebpackPlugin(getHtmlConfig('orders_refund')),
        new HtmlWebpackPlugin(getHtmlConfig('orders_refundinfo')),
        new HtmlWebpackPlugin(getHtmlConfig('pages_edit_pwd')),
        new HtmlWebpackPlugin(getHtmlConfig('pages_login')),
        new HtmlWebpackPlugin(getHtmlConfig('pages_profile')),
        new HtmlWebpackPlugin(getHtmlConfig('productCate')),
        new HtmlWebpackPlugin(getHtmlConfig('sellers_manage')),
        new HtmlWebpackPlugin(getHtmlConfig('shops_manage')),
        new HtmlWebpackPlugin(getHtmlConfig('swiper_items')),
        new HtmlWebpackPlugin(getHtmlConfig('user_manage')),
        new webpack.HotModuleReplacementPlugin() 
    ],

    optimization: {
        minimize: false,
        minimizer: [
            new TerserPlugin({
                extractComments: false,
            }),
        ],
        splitChunks: {
            cacheGroups: {
                commons: {
                    name: 'utils',
                    chunks: 'all',
                    minChunks: 2,
                    minSize: 0
                },
            }
        }
    },
    resolve: {
        alias: {
            util: __dirname + '/src/util',
            page: __dirname + '/src/page',
            images: __dirname + '/src/images',
            service: __dirname + '/src/service',
            node_modules: __dirname + '/node_modules'
        }
    }
};

module.exports = config;